iOS转场动画之微信朋友圈图片查看器

前言

什么是转场动画呢?顾名思义,就是切换界面所用的动画效果。本文主要介绍的是modal的效果。当我们使用modal的时候,只需要使用 presentViewController:animated:completiondismissViewControllerAnimated:completion 即可实现界面的弹出和消失,但是这样的动画过于单调,只是简单的弹出消失,没有’艺术性’,嘿嘿嘿,如果我们想要炫酷复杂的动画,那就只有自定义动画了。

modal设置自定义转场步骤

1.设置属性和代理

属性

当我们使用普通的modal进行转场的时候,是这样的

//创建要跳转的控制器
CLHPhotoBrowserViewController *photoVC = [[CLHPhotoBrowserViewController alloc] init];
//跳转控制器
[self.window.rootViewController presentViewController:photoVC animated:YES completion:nil];

但是如果我们要自定义modal转场动画,那么需要设置ViewController的modalPresentationStyle属性为UIModalPresentationCustom.即自定义状态,因为系统默认的属性为UIModalPresentationFullScreen,即充满屏幕.

photoVC.modalPresentationStyle = UIModalPresentationCustom;
代理

从iOS7之后,苹果推出了真正可以实现转场动画的API,这才让我们有了那么多酷炫的界面效果和动画,转场呢,又分为交互式转场和非交互式转场,本文主要讲解非交互式转场,因为这种转场可以让我们完全控制转场所利用的界面和动画。

自定义转场其实也很简单,只是实现一个协议而已,如果我们使用modal进行转场,那么我们必须设置我们要跳转控制器的transitioningDelegate,而要成为跳转控制器的代理,需要遵守UIViewControllerTransitioningDelegate 协议.为了防止代码量太多而导致不好管理,所以说我一般自定义一个类,来管理转场动画,这里我们使用一个CLHPhotoBrowserAnimator类来管理转场动画.

//将自定义类设置为转场代理
photoVC.transitioningDelegate = self.animator;

2.实现代理方法

UIViewControllerTransitioningDelegate

UIViewControllerTransitioningDelegate 中,我们主要使用两个方法.

  • 弹出界面的回调方法
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return self;
}
  • 消失界面的回调方法
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return self;
}
UIViewControllerAnimatedTransitioning

从上面的回调方法中我们发现,返回的对象必须遵守UIViewControllerAnimatedTransitioning 协议,因为上面的协议UIViewControllerTransitioningDelegate 是来设置动画的执行者的,而UIViewControllerAnimatedTransitioning 才是设置动画的,所以说,我们自定义的CLHPhotoBrowserAnimator必须遵守此协议,并实现其代理方法.

  • 设置转场动画的执行时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.5;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值